package com.xcn.dsxm.main_public.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Util_JsonToItem {

    /**
     * JSONObject转HashMap
     */
    public static HashMap getHashMapByJSONObject(JSONObject json){
        HashMap params=new HashMap();
        Iterator iterator=json.keySet().iterator();
        while (iterator.hasNext()){
            String key= (String) iterator.next();
            if (json.containsKey(key)){
                Object object=json.get(key);
                params.put(key,object);
            }
            else {
                return null;
            }
        }
        return params;
    }

    /**
     * JSONArray转List嵌套HashMap
     */
    public static List<HashMap> getListByJSONArray(JSONArray jsonArray){
        List<HashMap> list=new LinkedList<HashMap>();
        HashMap params=new HashMap();
        for (Object json:jsonArray){
            params=getHashMapByJSONObject((JSONObject)json);
            list.add(params);
            params.clear();
        }
        return list;
    }

}
